package com.ruoyi.web.controller.projectAch;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.projectAch.domain.GrantingUnit;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.projectAch.domain.AchievementInfo;
import com.ruoyi.projectAch.service.IAchievementInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 成果信息Controller
 * 
 * @author ruoyi
 * @date 2023-11-07
 */
@RestController
@RequestMapping("/projectAch/info")
public class AchievementInfoController extends BaseController
{
    @Autowired
    private IAchievementInfoService achievementInfoService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询成果信息列表
     */
    @PreAuthorize("@ss.hasPermi('projectAch:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(AchievementInfo achievementInfo)
    {
        startPage();
        List<AchievementInfo> list = achievementInfoService.selectAchievementInfoList(achievementInfo);
        return getDataTable(list);
    }

    /**
     * 导出成果信息列表
     */
    @PreAuthorize("@ss.hasPermi('projectAch:info:export')")
    @Log(title = "成果信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AchievementInfo achievementInfo)
    {
        List<AchievementInfo> list = achievementInfoService.selectAchievementInfoList(achievementInfo);
        ExcelUtil<AchievementInfo> util = new ExcelUtil<AchievementInfo>(AchievementInfo.class);
        util.exportExcel(response, list, "成果信息数据");
    }

    /**
     * 获取成果信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('projectAch:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(achievementInfoService.selectAchievementInfoById(id));
    }

    /**
     * 新增成果信息
     */
    @PreAuthorize("@ss.hasPermi('projectAch:info:add')")
    @Log(title = "成果信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AchievementInfo achievementInfo)
    {
        return toAjax(achievementInfoService.insertAchievementInfo(achievementInfo));
    }

    /**
     * 修改成果信息
     */
    @PreAuthorize("@ss.hasPermi('projectAch:info:edit')")
    @Log(title = "成果信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AchievementInfo achievementInfo)
    {
        return toAjax(achievementInfoService.updateAchievementInfo(achievementInfo));
    }

    /**
     * 删除成果信息
     */
    @PreAuthorize("@ss.hasPermi('projectAch:info:remove')")
    @Log(title = "成果信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(achievementInfoService.deleteAchievementInfoByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('projectAch:info:query')")
    @GetMapping(value = "/queryGrantingUnit")
    public AjaxResult queryGrantingUnit()
    {
        return success(achievementInfoService.queryGrantingUnit());
    }

    @PreAuthorize("@ss.hasPermi('projectAch:info:query')")
    @GetMapping(value = "/querySchoolName")
    public AjaxResult querySchoolName()
    {
        return success(achievementInfoService.querySchoolName());
    }

    @Log(title = "导入成果信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<AchievementInfo> util = new ExcelUtil<AchievementInfo>(AchievementInfo.class);
        List<AchievementInfo> userList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = achievementInfoService.importUser(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
        return util.importTemplateExcel("用户数据");
    }

}
